package com.akaovi.chatroom.scene;

import com.akaovi.chatroom.client.Client;
import javafx.application.Platform;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Scene;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import org.kordamp.ikonli.javafx.FontIcon;

import java.util.Optional;

/**
 * @author akaovi
 * @version 1.0
 * @description 主界面
 * @date 2022/12/9 0:24
 * @since 2022/12/9 0:24
 */
public class MainScene {
    static double mainWidth = 700;
    static double mainHeight = 500;
    static GridPane gridPane = new GridPane();  // 主布局
    static Scene mainScene = new Scene(gridPane, mainWidth, mainHeight);
    static Image commonUser = new Image("common-user.png");
    static ImageView commonUserImg = new ImageView(commonUser);
    static Image commonUser2 = new Image("common-user2.png");
    static FontIcon messIcon = new FontIcon("anto-message");
    static FontIcon friendIcon = new FontIcon("anto-team");
    static FontIcon infoIcon = new FontIcon("anto-info-circle");
    static FontIcon addFriend = new FontIcon("anto-user-add");
    static FontIcon logoutIcon = new FontIcon("anto-logout");
    static FontIcon commonIcon = new FontIcon("anto-rocket");


    static {
        // 主界面初始化
        initial();
        // todo 调试
//        gridPane.setGridLinesVisible(true);
        commonUserImg.setPreserveRatio(true);
        commonUserImg.setFitHeight(65);
        commonUserImg.setFitWidth(45);
        gridPane.add(commonUserImg, 0, 0);

        messIcon.setIconSize(25);
        gridPane.add(messIcon, 0, 1);

        friendIcon.setIconSize(25);
        gridPane.add(friendIcon, 0, 2);

        addFriend.setIconSize(25);
        gridPane.add(addFriend, 0, 3);

        infoIcon.setIconSize(25);
        gridPane.add(infoIcon, 0, 8);

        logoutIcon.setIconSize(25);
        gridPane.add(commonIcon, 5, 3, 3, 3);

        commonIcon.setIconSize(100);
        gridPane.add(logoutIcon, 0, 9);

        // 头像
        commonUserImg.setOnMouseClicked(e -> {
            if ("common-user.png".equals(getFileName(commonUserImg.getImage().getUrl()))) {
                commonUserImg.setImage(commonUser2);
            } else {
                commonUserImg.setImage(commonUser);
            }
        });
        // 消息列表
        messIcon.setOnMouseClicked(e -> {
            if (!ChatList.chatList.isVisible()) {
                FriendsList.coverFriendsList();
                UserInfoBoard.coverUserInfo();
                InfoBoard.coverInfoBoard();
                ChatList.showChatList();
                SendMessBoard.showSendMessBoard();
            }
        });
        // 好友列表
        friendIcon.setOnMouseClicked(e -> {
            if (!FriendsList.friendsList.isVisible()) {
                SendMessBoard.coverSendMessBoard();
                ChatList.coverChatList();
                InfoBoard.coverInfoBoard();
                FriendsList.showFriendsList();
                UserInfoBoard.showUserInfo();
            }
        });
        // 添加好友
        addFriend.setOnMouseClicked(e -> toAddFriend());

        // 关于
        infoIcon.setOnMouseClicked(e -> {
            if (!InfoBoard.infoList.isVisible()) {
                SendMessBoard.coverSendMessBoard();
                UserInfoBoard.coverUserInfo();
                MainScene.coverCommonIcon();
                FriendsList.coverFriendsList();
                ChatList.coverChatList();
                InfoBoard.showInfoBoard();
            }
        });
        // 登出
        logoutIcon.setOnMouseClicked(e -> {
            if (Client.ot != null) {
                Client.ot.close();
            }
            LoginScene.changeToLoginScene();
            restart();
        });
    }

    /**
     * 添加好友事件绑定
     */
    private static void toAddFriend() {
        Platform.runLater(() -> {
            TextInputDialog dialog = new TextInputDialog();
            dialog.setTitle("添加好友");
            dialog.setContentText("请输入对方的账号: ");

            Optional<String> result = dialog.showAndWait();
            result.ifPresent(s -> Client.ot.sendToAddFriend(s, "我想加你为好友"));
        });
    }

    /**
     * 初始化布局
     */
    private static void initial() {
        // 行
        RowConstraints rowConstraints1 = new RowConstraints(50);
        rowConstraints1.setValignment(VPos.CENTER);
        gridPane.getRowConstraints().add(rowConstraints1);
        gridPane.getRowConstraints().add(rowConstraints1);
        gridPane.getRowConstraints().add(rowConstraints1);
        gridPane.getRowConstraints().add(rowConstraints1);
        gridPane.getRowConstraints().add(rowConstraints1);
        gridPane.getRowConstraints().add(rowConstraints1);
        gridPane.getRowConstraints().add(rowConstraints1);
        gridPane.getRowConstraints().add(rowConstraints1);
        gridPane.getRowConstraints().add(rowConstraints1);
        gridPane.getRowConstraints().add(rowConstraints1);

        // 列
        ColumnConstraints columnConstraints1 = new ColumnConstraints(70);
        ColumnConstraints columnConstraints2 = new ColumnConstraints(50);
        ColumnConstraints columnConstraints3 = new ColumnConstraints(90);
        columnConstraints1.setHalignment(HPos.CENTER);
        columnConstraints2.setHalignment(HPos.CENTER);
        columnConstraints3.setHalignment(HPos.CENTER);
        gridPane.getColumnConstraints().add(columnConstraints2);
        gridPane.getColumnConstraints().add(columnConstraints3);
        gridPane.getColumnConstraints().add(columnConstraints1);
        gridPane.getColumnConstraints().add(columnConstraints1);
        gridPane.getColumnConstraints().add(columnConstraints1);
        gridPane.getColumnConstraints().add(columnConstraints1);
        gridPane.getColumnConstraints().add(columnConstraints1);
        gridPane.getColumnConstraints().add(columnConstraints1);
        gridPane.getColumnConstraints().add(columnConstraints1);
        gridPane.getColumnConstraints().add(columnConstraints1);
    }

    /**
     * 转到主界面
     */
    public static void changeToMainScene() {
        Client.getStage().setScene(MainScene.mainScene);
    }

    /**
     * 获取文件名
     * @param path 路径
     * @return
     */
    private static String getFileName(String path) {
        String[] split = path.split("/");
        return split[split.length - 1];
    }

    /**
     * 展示主水印
     */
    public static void showCommonIcon() {
        if (!commonIcon.isVisible()) {
            commonIcon.setVisible(true);
        }
    }

    /**
     * 不展示
     */
    public static void coverCommonIcon() {
        if (commonIcon.isVisible()) {
            commonIcon.setVisible(false);
        }
    }

    /**
     * 重置
     */
    public static void restart() {
        ChatList.restart();
        FriendsList.restart();
        SendMessBoard.restart();
        UserInfoBoard.restart();
        // 重置面板
        SendMessBoard.coverSendMessBoard();
        UserInfoBoard.coverUserInfo();
        FriendsList.showFriendsList();
        ChatList.coverChatList();
        InfoBoard.coverInfoBoard();
        MainScene.showCommonIcon();
    }
}
